package org.erp.spu.controller;

import org.erp.common.domain.Result;
import org.erp.common.page.DataPageResp;
import org.erp.common.page.PageQueryModel;
import org.erp.spu.entity.bo.SpuReleaseGroupBo;
import org.erp.spu.entity.dto.SpuReleaseGroupDto;
import org.erp.spu.entity.vo.ReleaseGroupVo;
import org.erp.spu.entity.vo.SpuReleaseGroupVo;
import org.erp.spu.service.SpuReleaseGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

import static org.erp.common.domain.Result.success;
/**
 * 发布组表(SpuReleaseGroup)表控制层
 *
 * @author dingding
 * @since 2025-11-06 17:01:05
 */
@RestController
@RequestMapping("/releaseGroup")
public class SpuReleaseGroupController {
    @Resource
    private SpuReleaseGroupService spuReleaseGroupService;


    /**
     * 根据分类获取发布组列表以及发布组下的发布信息等
     */
    @GetMapping("getListByCategoryId")
    public Result<List<ReleaseGroupVo>> getListByCategoryId(@NotNull(message = "分类id不能为空") Long categoryId) {

        return Result.success(spuReleaseGroupService.getListByCategoryId(categoryId));
    }

    /**
     * 分页查询发布组
     */
    @GetMapping("getListByPage")
    public Result<DataPageResp<SpuReleaseGroupVo>> getListByPage(SpuReleaseGroupBo spuReleaseGroupBo) {
        SpuReleaseGroupDto dto = new SpuReleaseGroupDto();
        BeanUtils.copyProperties(spuReleaseGroupBo, dto);
        PageQueryModel<SpuReleaseGroupVo> queryModel = spuReleaseGroupService.getListByPage(dto);
        return success(DataPageResp.of(queryModel.getTotal(), queryModel.getDataList()));
    }

    /**
     * 获取发布组列表
     */
    @GetMapping("getList")
    public Result<List<SpuReleaseGroupVo>> getList(SpuReleaseGroupBo spuReleaseGroupBo) {
        return Result.success(spuReleaseGroupService.getList(spuReleaseGroupBo));
    }

    /**
     * 通过主键查询单条数据
     */
    @GetMapping("/{id}")
    public Result<SpuReleaseGroupVo> selectOne(@PathVariable Long id) {
        return Result.success(spuReleaseGroupService.getSpuReleaseGroupVoById(id));
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public Result<Boolean> insert(@RequestBody SpuReleaseGroupDto spuReleaseGroupDto) {
        boolean result = spuReleaseGroupService.addSpuReleaseGroup(spuReleaseGroupDto);
        return result ? Result.success("新增成功") : Result.error("新增失败");
    }

    /**
     * 修改数据
     */
    @PostMapping("/upd")
    public Result<Boolean> update(@RequestBody SpuReleaseGroupDto spuReleaseGroupDto) {
        boolean result = spuReleaseGroupService.updateSpuReleaseGroup(spuReleaseGroupDto);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 删除数据
     */
    @PostMapping("/del")
    public Result<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        boolean result = spuReleaseGroupService.removeByIds(idList);
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }



}
